package liill1.js_interface;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.attribution.AppsFlyerRequestListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static android.content.ContentValues.TAG;

import androidx.annotation.NonNull;

public class AppsflyerToJavascript {

    private static Context getApplicationContext;

    public AppsflyerToJavascript(Context context) {
        getApplicationContext = context;
    }

    /**
     * 自定义事件
     * @param json 事件名称参数
     *                name 事件值  key
     *                data 事件值  value
     */
    @JavascriptInterface
        public void tracker(String json) {
            try {
                JSONObject jsonObject = new JSONObject(json);
                String name = jsonObject.getString("name");
                String data = jsonObject.getString("data");
                Log.e(TAG, "postMessage name=" + name);
                Log.e(TAG, "postMessage data=" + data);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.DATE_A, data);
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.SEARCH, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }

                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        /**
         * 登录事件
         * @param json  登录事件参数
         *                  PARAM_1  1 登录成功
         *
         */
        @JavascriptInterface
        public void login(String json){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String param_1 = jsonObject.getString("PARAM_1");
                Log.e(TAG, "postMessage PARAM_1=" + param_1);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.PARAM_1, param_1);
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.LOGIN, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        /**
         *  注册事件
         * @param json 注册事件参数
         *                    REGION  地区    例如：CN
         */
        //TODO 可以配置更多参数
        @JavascriptInterface
        public void register(String json){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String region = jsonObject.getString("REGION");
                Log.e(TAG, "postMessage REGION=" + region);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.REGION, region); // 根据需要调整键和值
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.COMPLETE_REGISTRATION, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    /**
     * 购买事件
     * @param json 购买事件参数
     *                    货币  CURRENCY  例如：USD
     *                    收入  REVENUE       100
     */
    @JavascriptInterface
        public void firstPurchase(String json){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String revenue = jsonObject.getString("REVENUE");
                String currency = jsonObject.getString("CURRENCY");
                Log.e(TAG, "postMessage REVENUE=" + revenue);
                Log.e(TAG, "postMessage CURRENCY=" + currency);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.REVENUE, revenue);
                eventData.put(AFInAppEventParameterName.CURRENCY, currency);
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.PURCHASE, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        @JavascriptInterface
        public void secondaryCharging(String json){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String revenue = jsonObject.getString("REVENUE");
                String currency = jsonObject.getString("CURRENCY");
                Log.e(TAG, "postMessage REVENUE=" + revenue);
                Log.e(TAG, "postMessage CURRENCY=" + currency);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.REVENUE, revenue);
                eventData.put(AFInAppEventParameterName.CURRENCY, currency);
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.PURCHASE, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    /**
     * 复杂购买事件
     * @param json 购买事件参数
     *                         收入  REVENUE  例如：USD_1000
     *                         货币  CURRENCY      USD
     *                         数量  QUANTITY      10
     *                         内容ID  CONTENT_ID  1001
     *                         订单ID  ORDER_ID    10002012
     *                         收据ID  RECEIPT_ID  323213123
     */
    @JavascriptInterface
        public void af_purchaseMap(String json){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String region = jsonObject.getString("REVENUE");
                String currency = jsonObject.getString("CURRENCY");
                String quantity = jsonObject.getString("QUANTITY");
                String content_id = jsonObject.getString("CONTENT_ID");
                String order_id = jsonObject.getString("ORDER_ID");
                String receipt_id = jsonObject.getString("RECEIPT_ID");
                Log.e(TAG, "postMessage REVENUE=" + region);
                Log.e(TAG, "postMessage CURRENCY=" + currency);
                Log.e(TAG, "postMessage QUANTITY=" + quantity);
                Log.e(TAG, "postMessage CONTENT_ID=" + content_id);
                Log.e(TAG, "postMessage ORDER_ID=" + order_id);
                Log.e(TAG, "postMessage RECEIPT_ID=" + receipt_id);
                Map<String, Object> eventData = new HashMap<>();
                eventData.put(AFInAppEventParameterName.REVENUE, region);
                eventData.put(AFInAppEventParameterName.CURRENCY, region);
                eventData.put(AFInAppEventParameterName.QUANTITY, region);
                eventData.put(AFInAppEventParameterName.CONTENT_ID, region);
                eventData.put(AFInAppEventParameterName.ORDER_ID, region);
                eventData.put(AFInAppEventParameterName.RECEIPT_ID, region);
                AppsFlyerLib.getInstance().logEvent(getApplicationContext, AFInAppEventType.PURCHASE, eventData, new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("AFSuccess", "Event tracking request successfully sent to AF");
                    }

                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d("AFError", "code:-->"+i+"\n" +
                                "Event tracking request failed to send to AF: " + s);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }